log using DO_RA_ALL , replace
/**********************/
/*Clean and set memory*/
/**********************/
clear all
set matsize 5000
set more off

/******************/
/*Install programs*/
/******************/
/*
cap ssc install xtabond2 
cap ssc install xtivreg2 
cap ssc install spmat 
cap ssc install spmack
*/
/*********************/
/*Sets base directory*/
/*********************/
global project "D:\IMF_INE_2_2021"  /* Set base directory                         */

local  repsBS=100                                                                           /* Number of bootstrap repetitions            */

/****************************/
/*Create anxillary variables*/
/****************************/

use "$project/imf_ehii_code.dta"
xtset ccode1 year
rename ehii y 



/* Identifies transitions */
gen timfprogram=.
replace timfprogram=1 if imf_all==1&l.imf_all==0
replace timfprogram=0 if imf_all==0&l.imf_all==0  

/* Defines lags of inequality */
gen lag1y=l1.y
gen lag2y=l2.y
gen lag3y=l3.y
gen lag4y=l4.y                       

/* Drop observations with no data to estimate probit */
keep if lag1y!=. 
keep if lag2y!=.
keep if lag3y!=.
keep if lag4y!=.                     

/* Creates lags of inequality */


gen ydep1=L5.y-L.y
gen ydep2=L4.y-L.y
gen ydep3=L3.y-L.y
gen ydep4=L2.y-L.y
gen ydep5=0                        

/* Creates leads of inequality */
gen ydep6=y-L.y
gen ydep7=F1.y-L.y
gen ydep8=F2.y-L.y
gen ydep9=F3.y-L.y
gen ydep10=F4.y-L.y
gen ydep11=F5.y-L.y
  gen ydep12=F6.y-L.y
  gen ydep13=F7.y-L.y
  gen ydep14=F8.y-L.y
  gen ydep15=F9.y-L.y

/* Define sample */
keep if timfprogram!=.	

/* Set seed */				 
set seed 12345                       
                          
                        
                      

/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
/*********************DEFINE PROGRAM THAT COMPUTES THE REGRESSION ADJUSTMENT ESTIMATOR FOR ALL YEARS  **************************/
/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
capture program tefpas_ra, eclass

/* Create year dummies and avoids using ones with no observation (program teffects gets stuck with variables it should drop) */
quietly: tab year if ydep1!=. & timfprogram!=., gen(dumyears)

/* Estimates for year -5 */
cap: teffects ra (ydep1 lag1y lag2y lag3y lag4y dumyears*, noconstant) (timfprogram), atet iterate(7)

/* Save the results */
if _rc==0{
matrix b=_b[ATET: r1vs0.timfprogram]
}
else{
matrix b=(.)
}
quietly: drop dumyears*



/* Estimates for years -4, -3, -2, -1 are zero by construction */
matrix b=(b, 0)
matrix b=(b, 0)
matrix b=(b, 0)
matrix b=(b, 0)

/* Estimates for years 0 to 10 */
forvalues s=6(1)15{

/* Create year dummies and avoids using ones with no observation (program teffects gets stuck with variables it should drop) */
quietly: tab year if ydep`s'!=. & timfprogram!=., gen(dumyears)

/* Estimates for year s */
cap: teffects ra (ydep`s' lag1y lag2y lag3y lag4y dumyears*, noconstant) (timfprogram), atet iterate(7)

/* Save the results */
if _rc==0{
matrix b=(b, _b[ATET: r1vs0.timfprogram])
}
else{
matrix b=(b, .)
}
quietly: drop dumyears*
}

/* Post results in matrix b, variance computed by bootstrapping later */
ereturn post b
end





/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
/************************************ ESTIMATION PROCEDURES AND STORING RESULTS ************************************************/
/*******************************************************************************************************************************/
/*******************************************************************************************************************************/

/* 1. Bootstrap and save results */
xtset, clear
bootstrap _b, reps(`repsBS') cluster(ccode1): tefpas_ra 
parmest, format(estimate min95 max95) saving("$project\resultsAll\impulse_ra_alt", replace)

/* Results, 5-1 years before transition */
estimates store e0
nlcom (effect0: (_b[c1]+_b[c2]+_b[c3]+_b[c4]+_b[c5])/5), post
estimates store e1
/* Results, 1-5 years after transition */
estimates restore e0
nlcom (effect0: (_b[c6]+_b[c7]+_b[c8]+_b[c9]+_b[c10])/5), post
estimates store e2

/* Results, 6-11 years after transition */
estimates restore e0
nlcom (effect0: (_b[c11]+_b[c12]+_b[c13]+_b[c14]+_b[c15])/5), post
estimates store e3

log close

 translate DO_RA_ALL.smcl DO_RA_ALL.pdf